package com.example.meterialproject.activitys

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.example.meterialproject.R
import com.example.meterialproject.base.BaseActivity
import com.example.meterialproject.databinding.ActivityDatePickerBinding
import com.example.meterialproject.exts.*
import com.example.meterialproject.exts.szj.szj
import com.google.android.material.datepicker.CalendarConstraints
import com.google.android.material.datepicker.DateValidatorPointForward
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.slider.Slider
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.util.*

/**
 *
 * @ClassName: DatePickerActivity
 * @Author: android 超级兵
 * @CreateDate: 4/25/22$ 9:25 AM$
 * TODO 日期选择器页面
 * 参考自materialDesign官方源码
 */
class DatePickerActivity : BaseActivity<ActivityDatePickerBinding>() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.btnShow.click {

//            createFlowDatePicker()

            createOriginDatePicker()


            // 滑动监听
            binding.slider.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
                override fun onStartTrackingTouch(slider: Slider) {
                    slider showSnack "onStartTrackingTouch"
                }

                override fun onStopTrackingTouch(slider: Slider) {
                    slider showSnack "onStopTrackingTouch"
                }
            })
        }

<<<<<<< HEAD
=======
        }

>>>>>>> dev
        // 时间选择
        binding.btnTimePicker.click {
            createTimePicker()
        }
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 4/29/22 10:14 AM
     * TODO 原始效果
     */
    private fun createOriginDatePicker() {
        when (binding.chipGroupSingle.getSelectIndex()) {
            1 -> { // 单选
                MaterialDatePicker.Builder.datePicker()
            }
            else -> { // 多选
                MaterialDatePicker.Builder.dateRangePicker()
            }
        }.apply {
            setType(this)
        }.build().apply {
            // 确定按钮响应
            addOnPositiveButtonClickListener {
                this@DatePickerActivity toast "时间戳:$it\n时间:$headerText"
            }

            addOnNegativeButtonClickListener {
                this@DatePickerActivity toast "关闭"
            }

            addOnCancelListener {
                this@DatePickerActivity toast "确定"
            }
            show(supportFragmentManager, TAG)
        }
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 4/25/22 8:33 PM
     * TODO 时间选择器
     */
    private fun createTimePicker() {
        val timePicker = MaterialTimePicker.Builder()
            // 默认
            .setTimeFormat(TimeFormat.CLOCK_12H)
            .setHour(10) // 默认小时
            .setMinute(20) // 默认分钟
            .build()


        // 确定监听
        timePicker.addOnPositiveButtonClickListener {
            this toast "${timePicker.hour}-${timePicker.minute}"
        }


        timePicker.show(supportFragmentManager, TAG)
    }


    /*
     * 作者:android 超级兵
     * 创建时间: 4/25/22 8:22 PM
     * TODO 创建flow日期选择
     */
    private fun createFlowDatePicker() {
        lifecycleScope.launch {
            flow {
                when (binding.chipGroupSingle.getSelectIndex()) {
                    1 -> { // 单选
                        emit(MaterialDatePicker.Builder.datePicker())
                    }
                    else -> { // 多选
                        emit(MaterialDatePicker.Builder.dateRangePicker())
                    }
                }
            }
                .map {
                    setType(it)
                    it
                }
                .map {
                    it.build()
                }
                .map {
                    // 添加监听
                    it.addOnPositiveButtonClickListener { element ->
                        this@DatePickerActivity toast "时间戳:$element\n时间:${it.headerText}"
                    }
                    it
                }.map {
                    it.addOnNegativeButtonClickListener {
                        this@DatePickerActivity toast "关闭"
                    }
                    it
                }.map {
                    it.addOnCancelListener {
                        this@DatePickerActivity toast "确定"
                    }

                    it
                }.collectLatest {
                    it.show(supportFragmentManager, TAG)
                }
        }
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 4/25/22 3:16 PM
     * TODO 设置样式
     */
    private fun <T> setType(materialDatePicker: MaterialDatePicker.Builder<T>) {
        // 设置样式
        when (binding.chipGroupTheme.getSelectIndex()) {
            1 -> { // 默认样式
                szj.getTheme(R.attr.materialCalendarTheme)?.let {
                    materialDatePicker.setTheme(it)
                }
            }
            else -> { // 全屏样式
                szj.getTheme(R.attr.materialCalendarFullscreenTheme)?.let {
                    materialDatePicker.setTheme(it)
                }
            }
        }

        when (binding.chipGroupHead.getSelectIndex()) {
            1 -> { // 选择
                materialDatePicker.setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR)
            }
            else -> { // 输入
                materialDatePicker.setInputMode(MaterialDatePicker.INPUT_MODE_TEXT)
            }
        }

        when (binding.chipGroupTime.getSelectIndex()) {
            1 -> {
                // 全都可以选
            }
            2 -> {
                // 今天之后 【今天之前都不可点击】
                setDateValidator(materialDatePicker, DateValidatorPointForward.now())
            }
            3 -> {
                // 2022-5-1 之后不可点击
                setDateValidator(materialDatePicker,
                    DateValidatorPointForward.from("2022-5-1 12:12:12".szj.dateToStamp()))
            }
        }

        when (binding.chipGroupOpen.getSelectIndex()) {
            1 -> {
                // 当前月
            }
            2 -> {
                // 下一月
                val calendar = Calendar.getInstance()
                // 当前时间
                calendar.timeInMillis = MaterialDatePicker.todayInUtcMilliseconds()
                // 设置一个月以后
                calendar.roll(Calendar.MONTH, 1)
                // 转换为时间戳
                val nextMonth = calendar.timeInMillis

                val constraintsBuilder = CalendarConstraints.Builder()
                // 设置默认展开
                constraintsBuilder.setOpenAt(nextMonth)
                materialDatePicker.setCalendarConstraints(constraintsBuilder.build())
            }
        }

        materialDatePicker.setTitleText("我是一只很长很长的标题")

    }

    /*
     * 作者:android 超级兵
     * 创建时间: 4/25/22 4:47 PM
     * TODO 设置今天之后不可点击
     */
    private fun <T> setDateValidator(
        materialDatePicker: MaterialDatePicker.Builder<T>,
        dateValidator: DateValidatorPointForward,
    ) {
        val constraintsBuilder = CalendarConstraints.Builder()
        constraintsBuilder.setValidator(dateValidator)
        materialDatePicker.setCalendarConstraints(constraintsBuilder.build())
    }
}